
-- our main need for this file is to track, within a given macro, exactly (or possibly in the case of cast/userandom) what spell or item is going to be used
-- next.  To do this, we need to parse each line of the given macro for certain things.
-- the ideal situation is when we have a #showtooltip and #show line with arguments, since this automatically makes life easy.

-- for the icon, determine if the #show [spell] line exists.  if so, then use the indicated spell.
-- next find out if either the #show line exists, or if the macro's icon is set to the ? mark icon.  if so we need to parse the rest.
-- otherwise simply return the icon of the macro chosen.  Embedded macros will use the override icon setting in InfiniBar.

-- for the tooltip, determine if the #showtooltip [spell] line exists.  if so, use the indicated spell as the tooltip.
-- next find out if the #showtooltip line exists.  if so we need to parse the rest.
-- otherwise simply set up the tooltip as WoW does.

local IB_SlashList = {}

function IB_GetMacroIcon(macro)
	local texture, texture2
	if type(macro) == "number" then
		texture2 = select(2,GetMacroInfo(macro))
		macro = select(3,GetMacroInfo(macro))
	end
	-- here we need to iterate through each line in the macro, and provide "none"; "spell", spellname; or "item", hyperlink
	for i,line in ipairs({strsplit("\n",macro)}) do
		-- first we need to split the line into command and options
		local _, _, cmd, opts = strfind(line,"/(%S+)%s+(.*)")
		if cmd then
			cmd = string.lower(cmd)
			if IB_SlashList[cmd] and IB_SlashList[cmd].textureFunc then
				texture = IB_SlashList[cmd].textureFunc(opts)
			end
			--DEFAULT_CHAT_FRAME:AddMessage("Icon check: cmd = "..tostring(cmd)..", opts = "..tostring(opts)..", texture = "..tostring(texture))
			if texture then
				return texture
			end
		end
	end
	--return texture2
end

function IB_GetEffectiveMacroInfo(macro)
	if type(macro) == "number" then
		macro = select(3,GetMacroInfo(macro))
	end
	-- here we need to iterate through each line in the macro, and provide "none"; "spell", spellname; or "item", hyperlink
	local mType, mData
	for i,line in ipairs({strsplit("\n",macro)}) do
		-- first we need to split the line into command and options
		local _, _, cmd, opts = strfind(line,"/(%S+)%s+(.*)")
		if cmd then
			cmd = string.lower(cmd)
			if IB_SlashList[cmd] and IB_SlashList[cmd].codeFunc then
				mType, mData = IB_SlashList[cmd].codeFunc(opts)
			end
			if mType then
				return mType, mData
			end
		end
	end
end

function IB_SetMacroTooltip(macro)
	local name, _
	if type(macro) == "number" then
		name, _, macro = GetMacroInfo(macro)
	end
	local set, rets
	-- here we need to iterate through each line in the macro, and provide "none"; "spell", spellname; or "item", hyperlink
	for i,line in ipairs({strsplit("\n",macro)}) do
		-- first we need to split the line into command and options
		local _, _, cmd, opts = strfind(line,"/(%S+)%s+(.*)")
		if cmd then
			cmd = string.lower(cmd)
			if IB_SlashList[cmd] and IB_SlashList[cmd].tooltipFunc then
				set, rets = IB_SlashList[cmd].tooltipFunc(opts)
			end
			if set then
				return unpack(rets)
			else
				-- set the tooltip here... for now just put some bogus shit in here.
				GameTooltip:ClearLines()
				GameTooltip:SetText(name or "Embedded Macro")
			end
		end
	end
end

local function RegisterSlashFuncs(slash,textureFunc,tooltipFunc,codeFunc)
	IB_SlashList[slash] = {
		textureFunc = textureFunc,
		tooltipFunc = tooltipFunc,
		codeFunc = codeFunc
	}
end

RegisterSlashFuncs("startattack",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then return GetSpellTexture("Attack()") end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then return true, {GameTooltip:SetSpell("Attack()")} end
	end,
	function(msg) -- codeFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then return "spell","Attack()" end
	end)

RegisterSlashFuncs("cast",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			local iteminfo = {GetItemInfo(action)}
			if iteminfo[1] then
				return iteminfo[10]
			else
				return GetSpellTexture(action)
			end
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			local iteminfo = {GetItemInfo(action)}
			if iteminfo[1] then
				return true, {GameTooltip:SetHyperlink(iteminfo[2])}
			else
				return true, {GameTooltip:SetSpell(InfiniBar:GetSpellInfo(action))}
			end
		end
	end,
	function(msg) -- codeFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			local iteminfo = {GetItemInfo(action)}
			if iteminfo[1] then
				return "item", iteminfo[2]
			else
				return "spell", action
			end
		end
	end)

RegisterSlashFuncs("castrandom",
	function(msg) -- textureFunc
		local actions, target = SecureCmdOptionParse(msg)
		if actions then
			local _,_, action = strfind(actions,"^([^,]+)")
			local iteminfo = {GetItemInfo(action)}
			if iteminfo[1] then
				return iteminfo[10]
			else
				return GetSpellTexture(action)
			end
		end
	end,
	function(msg) -- tooltipFunc
		local actions, target = SecureCmdOptionParse(msg)
		if actions then
			local _,_, action = strfind(actions,"^([^,]+)")
			local iteminfo = {GetItemInfo(action)}
			if iteminfo[1] then
				return true, {GameTooltip:SetHyperlink(iteminfo[2])}
			else
				return true, {GameTooltip:SetSpell(action)}
			end
		end
	end,
	function(msg) -- codeFunc
		local actions, target = SecureCmdOptionParse(msg)
		if actions then
			local _,_, action = strfind(actions,"^([^,]+)")
			local iteminfo = {GetItemInfo(action)}
			if iteminfo[1] then
				return "item", iteminfo[2]
			else
				return "spell", action
			end
		end
	end)

RegisterSlashFuncs("castsequence",
	function(msg) -- textureFunc
		local sequence, target = SecureCmdOptionParse(msg)
		if sequence then
			local _, item, spell = QueryCastSequence(sequence)
			if item then
				return select(10,GetItemInfo(item))
			else
				return GetSpellTexture(spell)
			end
		end
	end,
	function(msg) -- tooltipFunc
		local sequence, target = SecureCmdOptionParse(msg)
		if sequence then
			local _, item, spell = QueryCastSequence(sequence)
			if item then
				return true, {GameTooltip:SetHyperlink(select(2,GetItemInfo(item)))}
			else
				return true, {GameTooltip:SetSpell(InfiniBar:GetSpellInfo(spell))}
			end
		end
	end,
	function(msg) -- codeFunc
		local sequence, target = SecureCmdOptionParse(msg)
		if sequence then
			local _, item, spell = QueryCastSequence(sequence)
			if item then
				return "item", select(2,GetItemInfo(item))
			else
				return "spell", spell
			end
		end
	end)

RegisterSlashFuncs("equip",
	function(msg) -- textureFunc
		local item = SecureCmdOptionParse(msg)
		if item then
			return select(10,GetItemInfo(item))
		end
	end,
	function(msg) -- tooltipFunc
		local item = SecureCmdOptionParse(msg)
		if item then
			return true, {GameTooltip:SetHyperlink(select(2,GetItemInfo(item)))}
		end
	end,
	function(msg) -- tooltipFunc
		local item = SecureCmdOptionParse(msg)
		if item then
			return "item", select(2,GetItemInfo(item))
		end
	end)

RegisterSlashFuncs("equipslot",
	function(msg) -- textureFunc
		local action = SecureCmdOptionParse(msg)
		if action then
			local slot, item = strmatch(action, "^(%d+)%s*(.*)")
			if item then
				return select(10,GetItemInfo(item))
			end
		end
	end,
	function(msg) -- tooltipFunc
		local action = SecureCmdOptionParse(msg)
		if action then
			local slot, item = strmatch(action, "^(%d+)%s*(.*)")
			if item then
				return true, {GameTooltip:SetHyperlink(select(2,GetItemInfo(item)))}
			end
		end
	end,
	function(msg) -- tooltipFunc
		local action = SecureCmdOptionParse(msg)
		if action then
			local slot, item = strmatch(action, "^(%d+)%s*(.*)")
			if item then
				return "item", select(2,GetItemInfo(item))
			end
		end
	end)

RegisterSlashFuncs("use",
	function(msg) -- textureFunc
		local item, target = SecureCmdOptionParse(msg)
		if item then
			local bag, slot = strmatch(item, "^(%d+)%s+(%d+)$")
			if not bag then
				slot = strmatch(item, "^(%d+)$");
			end
			if bag then
				return select(10,GetItemInfo(GetContainerItemLink(bag, slot)))
			elseif slot then
				return select(10,GetItemInfo(GetInventoryItemLink("player",slot)))
			else
				return select(10,GetItemInfo(item))
			end
		end
	end,
	function(msg) -- tooltipFunc
		local item, target = SecureCmdOptionParse(msg)
		if item then
			local bag, slot = strmatch(item, "^(%d+)%s+(%d+)$")
			if not bag then
				slot = strmatch(item, "^(%d+)$");
			end
			if bag then
				return true, {GameTooltip:SetHyperlink(GetContainerItemLink(bag, slot))}
			elseif slot then
				return true, {GameTooltip:SetHyperlink(GetInventoryItemLink("player",slot))}
			else
				return true, {GameTooltip:SetHyperlink(select(2,GetItemInfo(item)))}
			end
		end
	end,
	function(msg) -- tooltipFunc
		local item, target = SecureCmdOptionParse(msg)
		if item then
			local bag, slot = strmatch(item, "^(%d+)%s+(%d+)$")
			if not bag then
				slot = strmatch(item, "^(%d+)$");
			end
			if bag then
				return "item", GetContainerItemLink(bag, slot)
			elseif slot then
				return "item", GetInventoryItemLink("player",slot)
			else
				return "item", select(2,GetItemInfo(item))
			end
		end
	end)

RegisterSlashFuncs("userandom",
	function(msg) -- textureFunc
		local items, target = SecureCmdOptionParse(msg)
		if items then
			local _,_, item = strfind(items,"^([^,]+)")
			local bag, slot = strmatch(item, "^(%d+)%s+(%d+)$")
			if not bag then
				slot = strmatch(item, "^(%d+)$");
			end
			if bag then
				return select(10,GetItemInfo(GetContainerItemLink(bag, slot)))
			elseif slot then
				return select(10,GetItemInfo(GetInventoryItemLink("player",slot)))
			else
				return select(10,GetItemInfo(item))
			end
		end
	end,
	function(msg) -- tooltipFunc
		local items, target = SecureCmdOptionParse(msg)
		if items then
			local _,_, action = strfind(items,"^([^,]+)")
			local bag, slot = strmatch(item, "^(%d+)%s+(%d+)$")
			if not bag then
				slot = strmatch(item, "^(%d+)$");
			end
			if bag then
				return true, {GameTooltip:SetHyperlink(GetContainerItemLink(bag, slot))}
			elseif slot then
				return true, {GameTooltip:SetHyperlink(GetInventoryItemLink("player",slot))}
			else
				return true, {GameTooltip:SetHyperlink(select(2,GetItemInfo(item)))}
			end
		end
	end,
	function(msg) -- tooltipFunc
		local items, target = SecureCmdOptionParse(msg)
		if items then
			local _,_, action = strfind(items,"^([^,]+)")
			local bag, slot = strmatch(item, "^(%d+)%s+(%d+)$")
			if not bag then
				slot = strmatch(item, "^(%d+)$");
			end
			if bag then
				return "item", GetContainerItemLink(bag, slot)
			elseif slot then
				return "item", GetInventoryItemLink("player",slot)
			else
				return "item", select(2,GetItemInfo(item))
			end
		end
	end)

RegisterSlashFuncs("petattack",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "Interface\\Icons\\Ability_GhoulFrenzy"
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return true, {GameTooltip:SetPetAction(1)}
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "pet", 1
		end
	end)

RegisterSlashFuncs("petfollow",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "Interface\\Icons\\Ability_Tracking"
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return true, {GameTooltip:SetPetAction(2)}
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "pet", 2
		end
	end)

RegisterSlashFuncs("petstay",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "Interface\\Icons\\Spell_Nature_TimeStop"
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return true, {GameTooltip:SetPetAction(3)}
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "pet", 3
		end
	end)

RegisterSlashFuncs("petpassive",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "Interface\\Icons\\Ability_Seal"
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return true, {GameTooltip:SetPetAction(8)}
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "pet", 8
		end
	end)

RegisterSlashFuncs("petdefensive",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "Interface\\Icons\\Ability_Defend"
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return true, {GameTooltip:SetPetAction(9)}
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "pet", 9
		end
	end)

RegisterSlashFuncs("petaggressive",
	function(msg) -- textureFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "Interface\\Icons\\Ability_Racial_BloodRage"
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return true, {GameTooltip:SetPetAction(10)}
		end
	end,
	function(msg) -- tooltipFunc
		local action, target = SecureCmdOptionParse(msg)
		if action then
			return "pet", 10
		end
	end)
